package com.mobisys.live.util;

import com.mobisys.live.dao.mapper.TagMapper;
import com.mobisys.live.dao.model.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class TagUtil {


    @Autowired
    private static TagMapper tagMapper;

    /**
     * 通过id串来获取标签串
     * @param idStr
     * @return
     */
    public static List<String> convertIdStrToTags(String idStr){
        System.out.println("idStr:"+idStr);
        if(idStr==null||idStr.isEmpty()){
            return null;
        }
         List<Integer> idList=IdStrUtil.convertToIdList(idStr);
         List<String> list=new ArrayList<>();

        for (int i = 0; i < idList.size(); i++) {
            Tag tag=tagMapper.selectByPrimaryKey(idList.get(i));
            list.add(tag.getTag());
        }
        return list;
    }

    /**
     * 插入标签串，返回id拼接串
     * @param tagList
     * @return
     */
    public static String insertTagListToDatabase(List<String> tagList){
        String result="";


        List<Integer> list=new ArrayList<>();

        for (int i = 0; i < tagList.size(); i++) {
            Tag tag=new Tag();
            tag.setIsDelete((byte)0);
            tag.setTag(tagList.get(i));
            int id=tagMapper.insertAndGetId(tag);
            list.add(id);
        }

        result=IdStrUtil.convertToIdStr(list);

        return result;
    }
}
